package tool;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Scanner;
import util.Util;

public class Files { // NOTE: Cannot read/write more than one file at a time

    private FileOutputStream streamOut;
    private OutputStreamWriter fileOut;
    private FileInputStream streamIn;
    private InputStreamReader fileIn;
    private int readIndex;
    private String[] readIns;
    
    public Files() {
    }

    public boolean openNewWrite(String filename) {
        Util.console("[Engine]  Writing to new file.");
        try {
            streamOut = new FileOutputStream(filename + ".cfg");
            fileOut = new OutputStreamWriter(streamOut, "UTF-8");
            return true;
        } catch (Exception e) {
            Util.console(e);
            Util.console("[Engine]  Failure opening a new write.");
            return false;
        }
    }

    public boolean write(String str) {
        try {
            fileOut.write(str);
            return true;
        } catch (Exception e) {
            Util.console(e);
            Util.console("[Engine]  Failure writing string to file.");
            return false;
        }
    }
    
    public boolean closeWrite() {
        try {
            fileOut.close();
            streamOut.close();
            return true;
        } catch (Exception e) {
            Util.console(e);
            Util.console("[Engine]  Failure finishing a write.");
            return false;
        }
    }

    public boolean openNewRead(String filename) {
        try {
            streamIn = new FileInputStream(filename + ".cfg");
            fileIn = new InputStreamReader(streamIn, "UTF-8");
            String total = "";
            int ch = fileIn.read();
            while (ch != -1) {
                total += (char) ch;
                ch = fileIn.read();
            }
            Scanner scan = new Scanner(total);
            int size = 0;
            while (scan.hasNext()) {
                scan.next();
                size++;
            }
            readIns = new String[size];
            scan = new Scanner(total);
            int i = 0;
            while (scan.hasNext()) {
                readIns[i++] = scan.next();
            }
            return true;
        } catch (Exception e) {
            Util.console(e);
            Util.console("[Engine]  Failure opening a new read.");
            return false;
        }
    }
    
    public boolean canRead() {
        return (readIndex < readIns.length);
    }

    public String read() {
        if (canRead()) {
            String str = readIns[readIndex++];
            return str;
        } else /* !canRead() */ {
            return "";
        }
    }

    public int readAsInt() {
        String readIn = read();
        int result = Integer.parseInt(readIn);
        return result;
    }

    public boolean readAsBool() {
        String readIn = read();
        boolean result = Boolean.parseBoolean(readIn);
        return result;
    }

    public float readAsFloat() {
        String readIn = read();
        float result = Float.parseFloat(readIn);
        return result;
    }

    public double readAsDouble() {
        String readIn = read();
        double result = Double.parseDouble(readIn);
        return result;
    }
    
    public boolean closeRead() {
        readIndex = 0;
        readIns = null;
        try {
            fileIn.close();
            streamIn.close();
            return true;
        } catch (Exception e) {
            Util.console(e);
            Util.console("[Engine]  Failure finishing a read.");
            return false;
        }
    }
}